﻿using System;
using System.Windows.Forms;
using DesignerHelper.Addons;
using DesignerHelper.Help;
using SpreadsheetGear;

namespace WorkbookDesigner.Addons.WorkbookExplorer
{
    public partial class WorksheetOptions : UserControl, IMarine<IWorksheet>
    {
        private IWorksheet WorkOn { get; set; }

        private IWorksheetWindowInfo Info { get; set; }
        //todo 网格颜色 标签颜色选项

        public WorksheetOptions()
        {
            InitializeComponent();
        }

        #region 封装变量

        private bool DisplayFormula
        {
            get { return checkBoxWinFormulas.Checked; }
            set { checkBoxWinFormulas.Checked = value; }
        }

        private bool DisplayZeroValue
        {
            get { return checkBoxWinZeroValue.Checked; }
            set { checkBoxWinZeroValue.Checked = value; }
        }

        private bool DisplayOutline
        {
            get { return checkBoxWinOutline.Checked; }
            set { checkBoxWinOutline.Checked = value; }
        }

        private bool DisplayRowColumnHeader
        {
            get { return checkBoxWinRowColumnHeader.Checked; }
            set { checkBoxWinRowColumnHeader.Checked = value; }
        }

        private bool DisplayGridlines
        {
            get { return checkBoxWinGridlines.Checked; }
            set { checkBoxWinGridlines.Checked = value; }
        }

        private int GetWinZoomValue()
        {
            if (radioButtonZoom100P.Checked)
                return 100;
            if (radioButtonZoom200P.Checked)
                return 200;
            if (radioButtonZoom50P.Checked)
                return 50;
            if (radioButtonZoom75P.Checked)
                return 75;
            if (radioButtonZoomCustom.Checked)
                return Convert.ToInt32(numericUpDownZoomCustom.Value);
            return 1;
        }

        private void WinZommInit(int value)
        {
            switch (value)
            {
                case 100:
                    radioButtonZoom100P.Checked = true;
                    break;
                case 50:
                    radioButtonZoom50P.Checked = true;
                    break;
                case 200:
                    radioButtonZoom200P.Checked = true;
                    break;
                case 75:
                    radioButtonZoom75P.Checked = true;
                    break;
                default:
                    radioButtonZoomCustom.Checked = true;
                    CustomZoomInit(value);
                    break;
            }
        }

        private void CustomZoomInit(int value)
        {
            numericUpDownZoomCustom.Value = Convert.ToDecimal(value);
        }

        private bool SheetProtection
        {
            get { return checkBoxProtection.Checked; }
            set { checkBoxProtection.Checked = value; }
        }

        private void InitSheetVisibility(SheetVisibility sv)
        {
            switch (sv)
            {
                case SheetVisibility.Visible:
                    checkBoxHideSheet.Checked = false;
                    break;
                case SheetVisibility.Hidden:
                    checkBoxHideSheet.Checked = true;
                    break;
                case SheetVisibility.VeryHidden:
                    checkBoxHideSheet.Checked = true;
                    checkBoxHideSheet.Enabled = false;
                    break;
            }
        }

        private void SetSheetVisibility()
        {
            switch (WorkOn.Visible) //enum为值类型
            {
                case SheetVisibility.VeryHidden:
                    return;
                case SheetVisibility.Hidden:
                case SheetVisibility.Visible:

                    try
                    {
                        WorkOn.Visible = checkBoxHideSheet.Checked ? SheetVisibility.Hidden : SheetVisibility.Visible;
                    }
                    catch (InvalidOperationException)
                    {
                        //throw new InvalidOperationException("无法隐藏工作簿中唯一可见的工作表");
                        MesBox.ErrorMessage("无法隐藏工作簿中唯一可见的工作表");
                    }
                    return;
            }
        }

        #endregion

        public void TriggerInit(IWorksheet worksheet)
        {
            Info = worksheet.WindowInfo;
            WorkOn = worksheet;
            Init();
        }

        public void TriggerReset()
        {
            Init();
        }

        void IMarine<IWorksheet>.TriggerInit(IWorksheet sendIn)
        {
            TriggerInit(sendIn);
        }

        void IScv<IWorksheet>.TriggerReset()
        {
            TriggerReset();
        }

        void IScv<IWorksheet>.TriggerSet()
        {
            TriggerSet();
        }

        Control IScv<IWorksheet>.GetControl()
        {
            return this;
        }

        private void Init()
        {
            DisplayZeroValue = Info.DisplayZeros;
            DisplayFormula = Info.DisplayFormulas;
            DisplayGridlines = Info.DisplayGridlines;
            DisplayOutline = Info.DisplayOutline;
            DisplayRowColumnHeader = Info.DisplayHeadings;
            WinZommInit(Info.Zoom);

            SheetProtection = WorkOn.ProtectContents;

            InitSheetVisibility(WorkOn.Visible);
        }

        public void TriggerSet()
        {
            SetSheetVisibility(); //bug 无法更改非当前激活的工作表的显示选项,必须激活为当前工作表.
            Info.Zoom = GetWinZoomValue();
            Info.DisplayFormulas = checkBoxWinFormulas.Checked;
            Info.DisplayGridlines = DisplayGridlines;
            Info.DisplayHeadings = DisplayRowColumnHeader;
            Info.DisplayOutline = DisplayOutline;
            Info.DisplayZeros = DisplayZeroValue;
            CustomZoomInit(Info.Zoom);
        }

        private void radioButtonZoomCustom_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonZoomCustom.Checked)
                CustomZoomInit(Info.Zoom);
        }
    }
}